﻿using System;
using System.Web.UI;
using System.Configuration;
using System.Data;
using log4net;

namespace LogMonitorWeb
{
    public partial class Search : System.Web.UI.Page
    {
        #region "Private Variables"

        static string conString = ConfigurationManager.ConnectionStrings["SecLogConnectionString"].ToString();

        DataSet searchDs;
        DataSet searchLogType;
        DataSet searchLogName;

        //日志记录工具。
        ILog log;

        #endregion


        protected void Page_Load(object sender, EventArgs e)
        {
            log4net.Config.XmlConfigurator.Configure();
            log = LogManager.GetLogger(typeof(Search));

            if (!Page.IsPostBack)
            {
                dataBinding();
            }


        }

        private void dataBinding()
        {
            try
            {
                string searchString = "select DISTINCT logtype from logMessage";
                searchLogType = SqlHelper.SqlHelper.GetDataSet(searchString, conString);
                dlLogType.DataSource = searchLogType;
                dlLogType.DataValueField = "logtype";
                dlLogType.DataBind();

                searchString = "select DISTINCT logname from logMessage";
                searchLogName = SqlHelper.SqlHelper.GetDataSet(searchString, conString);
                dlLogName.DataSource = searchLogName;
                dlLogName.DataValueField = "logname";
                dlLogName.DataBind();

                searchString = "select DISTINCT logmachine from logMessage";
                searchLogName = SqlHelper.SqlHelper.GetDataSet(searchString, conString);
                dlLogMachine.DataSource = searchLogName;
                dlLogMachine.DataValueField = "logmachine";
                dlLogMachine.DataBind();

                Calendar1.SelectedDate = DateTime.Now;

            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
            }


        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                lblInfo.Visible = false;


                string searchString = "select id,logname,logtype,logmachine,[user],logDate,logTime from logMessage";

                if (dlLogName.SelectedValue != "" && dlLogType.SelectedValue != "")
                {
                    searchString = searchString + " where logname = '" + dlLogName.SelectedValue + "' and logtype ='" + dlLogType.SelectedValue + "'";
                }
                else if (dlLogName.SelectedValue != "" && dlLogType.SelectedValue == "")
                {
                    searchString = searchString + " where logname = '" + dlLogName.SelectedValue + "'";
                }
                else if (dlLogType.SelectedValue != "" && dlLogName.SelectedValue == "")
                {
                    searchString = searchString + " where logtype = '" + dlLogType.SelectedValue + "'";
                }

                //添加日期

                searchString = searchString + " and logDate = '" + Calendar1.SelectedDate.ToString("yyyy-M-d") + "'";

                //添加服务器名称
                searchString = searchString + " and logmachine = '" + dlLogMachine .SelectedValue  + "'";

                log.Info(searchString);

                searchDs = SqlHelper.SqlHelper.GetDataSet(searchString, conString);
                resultGView.DataSource = searchDs;
                resultGView.DataBind();

                if (searchDs.Tables[0].Rows.Count < 1)
                {
                    lblInfo.Visible = true;
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
            }
        }

    }
}